package com.example.springcloudwinter.mapper;

import com.example.springcloudwinter.bo.CartBo;
import com.example.springcloudwinter.bo.FoodBo;
import org.apache.ibatis.annotations.*;

import java.sql.SQLException;
import java.util.List;

@Mapper
public interface CartMapper {
    @Delete("DELETE FROM cart WHERE userId=#{userId} AND businessId=#{businessId} AND foodId=#{foodId}")
    int removeCart(@Param("userId") String userId, @Param("businessId") int businessId, @Param("foodId") int foodId)throws SQLException;
    
    @Update("UPDATE cart SET quantity=#{quantity} WHERE userId=#{userId} AND businessId=#{businessId} AND foodId=#{foodId}")
    int updateCart(@Param("userId") String userId, @Param("businessId") int businessId, @Param("foodId") int foodId,
                   @Param("quantity") int quantity)throws SQLException;
    
    @Insert("INSERT INTO cart (foodId,userId,businessId) VALUES (#{foodId},#{userId},#{businessId}) ")
    int saveCart(@Param("userId") String userId,@Param("businessId") int businessId,@Param("foodId") int foodId)throws SQLException;
    
    @Select("SELECT cartId,foodId,businessId,userId,quantity FROM cart WHERE userId=#{userId} AND businessId=#{businessId}")
    List<CartBo> listCart(@Param("businessId")int businessId, @Param("userId")String userId)throws SQLException;
    
    @Select("SElECT foodId,businessId,foodPrice,foodName,foodExplain,foodImg,remarks,quantity FROM cart NATURAL JOIN food " +
            "WHERE userId=#{userId} AND businessId=#{businessId}")
    List<FoodBo> getFoodById(@Param("businessId")int businessId, @Param("userId")String userId)throws SQLException;
    
    @Select("SELECT cartId,foodId,businessId,userId,quantity FROM cart WHERE userId=#{userId} AND businessId=#{businessId} " +
            "AND foodId=#{foodId}")
    CartBo listSpecificCart(@Param("businessId")int businessId, @Param("userId")String userId, @Param("foodId")
            int foodId)throws SQLException;
}
